package com.bestcem.xm.ticket.service.manager.record.dto;

import com.bestcem.xm.ticket.entity.mongo.TicketTemplate;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/18 20:57
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TemplateFieldDTO {
    /**
     * text
     */
    @JsonProperty(value = "text")
    private String text;

    /**
     * type
     */
    @JsonProperty(value = "type")
    private String type;

    /**
     * options
     */
    @JsonProperty(value = "options")
    private Map<Integer, String> options;

    public static TemplateFieldDTO getTextInstance(TicketTemplate.FieldInfo fieldInfo) {
        TemplateFieldDTO templateFieldDTO = new TemplateFieldDTO();
        templateFieldDTO.setText(fieldInfo.getText());
        templateFieldDTO.setType(fieldInfo.getType());
        return templateFieldDTO;
    }

    public static TemplateFieldDTO getSelectInstance(TicketTemplate.FieldInfo fieldInfo) {
        TemplateFieldDTO templateFieldDTO = new TemplateFieldDTO();
        templateFieldDTO.setText(fieldInfo.getText());
        templateFieldDTO.setType(fieldInfo.getType());
        List<TicketTemplate.FieldInfo.Option> options = fieldInfo.getOptions();
        Map<Integer, String> optionMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(options)) {
            for (TicketTemplate.FieldInfo.Option option : options) {
                optionMap.put(option.getValue(), option.getText());
            }
        }
        templateFieldDTO.setOptions(optionMap);
        return templateFieldDTO;
    }
}
